﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Settings;
using System.Data.SqlClient;
namespace Business
{
    public partial class crm_advertisment
    {
        public void UpdateStatus(string status)
        {
            using (SqlConnection conn = new SqlConnection(LocalSettings.ConnectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    UpdateStatus(status, cmd);
                }
            }
        }

        public void UpdateStatus(string status, SqlCommand cmd)
        {
            this.status = status;
            cmd.CommandText = string.Format("UPDATE crm_advertisment SET status = '{0}' WHERE advertisment_id = {1}", status, this.advertisment_id);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Parameters.Clear();
            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// Cập nhật số lượng quảng cáo đã gửi đi vào trường sent_advertisment
        /// </summary>
        /// <param name="msgSent"></param>
        /// <param name="cmd"></param>
        public void UpdateSent(int msgSent, SqlCommand cmd)
        {
            try
            {
                if (msgSent > 0)
                {
                    cmd.CommandText = string.Format("UPDATE crm_advertisment SET sent_advertisment = sent_advertisment + {0} WHERE advertisment_id = {1}", msgSent, this.advertisment_id);
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.Parameters.Clear();
                    cmd.ExecuteNonQuery();
                    this.sent_advertisment += msgSent;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
